package com.dxtx.widget;

import android.animation.ValueAnimator;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.FrameLayout;

import java.lang.ref.WeakReference;

/**
 * Created by user on 2016/7/15.
 */
public class ListenKeyBordFrameLayout extends FrameLayout {

    public ListenKeyBordFrameLayout(Context context) {
        super(context);
        postDelayed(new Runnable() {
            @Override
            public void run() {
                initFinish = true;
            }
        }, 2000);
        detector = new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                handler.removeMessages(1);
                singleTap = true;
                handler.sendEmptyMessageDelayed(1, 1000);
                return true;
            }

        });
    }

    private MHandler handler = new MHandler(this);

    static class MHandler extends Handler {
        WeakReference<ListenKeyBordFrameLayout> v;

        public MHandler(ListenKeyBordFrameLayout v) {
            this.v = new WeakReference<ListenKeyBordFrameLayout>(v);
        }

        @Override
        public void handleMessage(Message msg) {
            if (v.get() == null) {
                return;
            }
            switch (msg.what) {
                case 0:
                    v.get().setSingleTap(true);
                    break;
                case 1:
                    v.get().setSingleTap(false);
                    break;
                case 2:
                    v.get().requestAllowPostScroll(true);
                    break;
                case 3:
                    v.get().setCanScrollSelf(false);
                    break;
            }
        }
    }


    private int maxH = 0;
    private GestureDetector detector;


    private float mScrollY = 0;
    private float mTapY = 0;
    private static final long SMOOTH_SCROLL_DURING = 250;

    private boolean initFinish = false;
    private boolean singleTap = false;
    private boolean userClick = true;
    private boolean canScrollSelf = false;//能自己顶键盘


    public void setSingleTap(boolean singleTap) {
        this.singleTap = singleTap;
    }

    public void setCanScrollSelf(boolean canScrollSelf) {
        this.canScrollSelf = canScrollSelf;
    }

    public void requestAllowPostScroll(boolean allow) {
        this.userClick = allow;
        if (!allow) {
            handler.sendEmptyMessageDelayed(2, 1000);
        }
    }

    public void smoothScrollToY(float y) {
        ValueAnimator animator = ValueAnimator.ofFloat(mScrollY, y);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator a) {
                float v = (Float) a.getAnimatedValue();
                setY(mScrollY = v);
            }
        });
        animator.setDuration(SMOOTH_SCROLL_DURING);
        animator.start();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if (h > maxH) {
            maxH = h;
        }
        canScrollSelf = true;
        handler.removeMessages(3);
        handler.sendEmptyMessageDelayed(3, 1000);
    }


    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        Log.d("px", "onLayout" + changed + "," + left + "," + top + "," + right + "," + bottom);
        if ((mScrollY = getY()) != 0) {
            Log.d("px", "mScrollY" + mScrollY);
            smoothScrollToY(0);
            Log.d("px", "onKeyBordHide");
            return;
        }

        if (initFinish && userClick && singleTap && !canScrollSelf) {
            //如果刚单击,引起onLayout,又是用户触发,则视为软键盘弹起吧~~
            onKeyBordShow(changed, left, top, right, bottom);
            float y = maxH * 0.4f - mTapY;
            if (y < 0) {
                smoothScrollToY(y);
            }else{
                smoothScrollToY(-10);
            }
        }
    }

    private void onKeyBordShow(boolean changed, int left, int top, int right, int bottom) {
        Log.d("px", "onKeyBordShow,tap:" + mTapY + ",mScrollY=" + mScrollY);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            mTapY = ev.getY();
        }
        detector.onTouchEvent(ev);
        return super.dispatchTouchEvent(ev);
    }
}
